package servlets;

import htmlWriter.HtmlWriter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.mta.pokerstats.LoginHandler;
import edu.mta.pokerstats.Team;
import edu.mta.pokerstats.LoginHandler.ELoginStaus;
import edu.mta.pokerstats.Player;

/**
 * Servlet implementation class servletLogInSignUpLogOut
 * Name: registration
 */
public class servletLogInSignUpLogOut extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public servletLogInSignUpLogOut() {
        super(); 
    }
        	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		process(request, response);
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		process(request, response);
	}

	private void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		HtmlWriter htmlWriter = new HtmlWriter();
		String theAction = request.getParameter("action");
		String playerUsername = request.getParameter("playerUsername");
		if(theAction.equalsIgnoreCase("logInValidation"))
		{
			response.setContentType("application/json;charset=UTF-8");
			String playerPassword = request.getParameter("playerPassword");
			if(LoginHandler.isLoginValid(playerUsername, playerPassword) == ELoginStaus.OK)
			{
				response.getWriter().println("{\"canContinue\":\"true\"}");
			}
			else
			{
				response.getWriter().println("{\"canContinue\":\"false\"}");
			}
		}
		else
		{
			if(theAction.equalsIgnoreCase("LogIn"))
			{
				Player viewingPlayer = Player.getPlayerByUsername(playerUsername);
				request.getSession().setAttribute("viewingPlayer", viewingPlayer);
				response.getWriter().println(htmlWriter.MainPage(viewingPlayer.getId()));
			}
			else
			{
				if(theAction.equalsIgnoreCase("checkUserUsername"))
				{
					processUsernameCheckRequest(request, response);
				}
				else
				{
					if(theAction.equalsIgnoreCase("checkTeamName"))
					{
						processTeamNameCheckRequest(request, response);
					}
					else
					{
						if(theAction.equalsIgnoreCase("LogOut"))
						{
							request.getSession().removeAttribute("viewingPlayer");
						}
					}
				}
			}
		}
	}
	
	protected void processTeamNameCheckRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String teamName = request.getParameter("teamName");
		Team currTeam = Team.getTeamByName(teamName);
		if (currTeam.getName() != null){
			out.print("{\"isUnique\" : \"false\"}");			
		}
		else {
			out.print("{\"isUnique\" : \"true\"}");
		}
	}

	protected void processUsernameCheckRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String playerUsername = request.getParameter("playerUsername");
		Player currPlayer = Player.getPlayerByUsername(playerUsername);
		if (currPlayer.getUsername() != null){
			out.print("{\"isUnique\" : \"false\"}");			
		}
		else {
			out.print("{\"isUnique\" : \"true\"}");
		}
	}
}
